package com.yld.car.market.tab;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.yld.car.market.DealerHomePageActivity;
import com.yld.car.market.FindCarHomePageActivity;
import com.yld.car.market.HomeActivity;
import com.yld.car.market.MoreActivity;
import com.yld.car.market.MyCarMarketActivity;
import com.yld.car.market.MyInfoActivity;
import com.yld.car.market.R;

public class TabActivityGroup extends AbstractActivityGroup {
	// 加载的Activity的名字，LocalActivityManager就是通过这些名字来查找对应的Activity的。
	private static final String CONTENT_0 = "contentActivity0";
	private static final String CONTENT_1 = "contentActivity1";
	private static final String CONTENT_2 = "contentActivity2";
	private static final String CONTENT_3 = "contentActivity3";

	// private static final String CONTENT_4 = "contentActivity4";

	private SharedPreferences mSettings;
	private SharedPreferences.Editor mEditor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_tab);
		super.onCreate(savedInstanceState);
		mSettings = this.getSharedPreferences("person_state", MODE_PRIVATE);
		mEditor = mSettings.edit();
		((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);

		boolean flag = mSettings.getBoolean("flag", false);
		setContainerView(CONTENT_0, FindCarHomePageActivity.class);
		if (flag) {

			setContainerView(CONTENT_2, MyCarMarketActivity.class);
			initTabBarButtons(true);
		}

	}

	/**
	 * 找到自定义id的加载Activity的View
	 */
	@Override
	protected ViewGroup getContainer() {
		return (ViewGroup) findViewById(R.id.container);
	}

	/**
	 * 初始化按钮
	 */
	@Override
	protected void initTabBarButtons(boolean flag) {

		RadioButton btn1 = initTabBarButton(R.id.radio_button0);
		RadioButton btn2 = initTabBarButton(R.id.radio_button1);
		RadioButton btn3 = initTabBarButton(R.id.radio_button2);
		RadioButton btn4 = initTabBarButton(R.id.radio_button3);

		if (flag) {
			btn2.setChecked(true);
			btn1.setChecked(false);
			btn3.setChecked(false);
			btn4.setChecked(false);
		} else {

			btn1.setChecked(true);
			btn2.setChecked(false);
			btn3.setChecked(false);
			btn4.setChecked(false);
		}
	}

	/**
	 * 导航按钮被点击时，具体发生的变化
	 */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		new Thread(new Runnable() {

			@Override
			public void run() {
				mEditor.putBoolean("flag", false).commit();

			}
		}).start();
		if (isChecked) {
			switch (buttonView.getId()) {

			case R.id.radio_button0:

				setContainerView(CONTENT_0, FindCarHomePageActivity.class);

				break;

			case R.id.radio_button1:

				setContainerView(CONTENT_1, DealerHomePageActivity.class);

				break;

			case R.id.radio_button2:

				setContainerView(CONTENT_2, MyInfoActivity.class);

				break;
			case R.id.radio_button3:

				setContainerView(CONTENT_3, MoreActivity.class);

				break;

			default:
				break;
			}
		}
	}

}